#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char hex[16] = "0123456789ABCDEF";

void usage()
{
  printf("usage: chn2asc <filename>\n\n");
}

int main(int argc, char **argv)
{
  FILE *pFile, *outFile;
  int i, nValue;
  char outname[12] = "";
  char writeline[32];

  if (argc < 2) {
    usage();
    return 1;
  }

  if ((pFile = fopen(argv[1], "rb"))) {
    /* Dummy */
    for (i = 0; i < 8; i++)
      fread(&nValue, sizeof(nValue), 1, pFile);

    /* Create new outFile Name and open it for writing */
    for (i = 0; i < strlen(argv[1])-4; i++)
      sprintf(outname,"%s%c",outname,argv[1][i]);
    sprintf(outname,"%s.asc",outname);
    outFile = fopen(outname, "w");

    /* Values */
    for (i = 0; i < 4096; i++) {
      fread(&nValue, sizeof(nValue), 1, pFile);
      sprintf(writeline,"%d %d\n", i, nValue);
      fputs(writeline,outFile);
    }

    fclose(pFile);
    fclose(outFile);
  } else {
    perror(argv[1]);
    return 1;
  }
  
  return 0;
}
